﻿using System;
using System.CommandLine;
using System.IO;

namespace DemoCommandLine
{
    internal class Ls
    {
        public Ls(RootCommand rootCmd)
        {
            var dirName = new Argument<string>(name: "dirname");
            dirName.SetDefaultValue(Directory.GetCurrentDirectory());
            var subCom = new Command("ls", "print dir content") { dirName };
            subCom.SetHandler((dirNameValue) =>
            {
                Handle(dirNameValue, FileOrDir.All);
            }, dirName);
            rootCmd.Add(subCom);
        }

        public static void Handle(string dirNameValue, FileOrDir fileOrDir)
        {
            Console.SetWindowSize(300, 100);
            if (fileOrDir == FileOrDir.File || fileOrDir == FileOrDir.All)
            {

                string[] files = Directory.GetFiles(dirNameValue);
                if (files.Length == 0)
                {
                    return;
                }
                foreach (var item in files)
                {
                    Console.Write(Path.GetFileName(item) + " ");
                }
            }

            if (fileOrDir == FileOrDir.Dir || fileOrDir == FileOrDir.All)
            {

                string[] dirs = Directory.GetDirectories(dirNameValue);
                if (dirs.Length == 0)
                {
                    return;
                }
                foreach (var item in dirs)
                {
                    Console.Write(Path.GetFileName(item) + " ");
                }
            }
        }
    }
}
